namespace Yaaf.WP7.NativeAccess.Helper
{
    using System;
    using System.IO;

    public class ProvisionXmlFileOperations
    {
        private ProvisonXmlBuilder provison;

        private ProvisonXmlCharacteristic root;

        public ProvisionXmlFileOperations()
        {
            provison = new ProvisonXmlBuilder();
            root = new ProvisonXmlCharacteristic() { Type = "FileOperation" };
            provison.AddCharacteristic(root);
        }

        public void AddMakeDirOperation(string dirPath)
        {
            var makeDir = new ProvisonXmlCharacteristic() { Type = dirPath, Translation = "install" };
            makeDir.AddCharacteristic(new ProvisonXmlCharacteristic(){Type = "MakeDir"});
            root.AddCharacteristic(makeDir);
        }

        public void AddCopyOperation(string source, string dest, ProvisionXmlFileAttributes removeAttributes = ProvisionXmlFileAttributes.None)
        {
            var dir = Path.GetDirectoryName(dest);
            var destName = Path.GetFileName(dest);
            var dirC = new ProvisonXmlCharacteristic() { Type = dir, Translation = "install" };
            var fileCopy = new ProvisonXmlCharacteristic() { Type = destName };
            var copy = new ProvisonXmlCharacteristic() { Type = "Copy" };
            copy.AddParam(new ProvisonXmlParm() { Name = "Source", Value = source, Translation="install" });
            if (removeAttributes != ProvisionXmlFileAttributes.None)
            {
                var attStr = GetAttributeString(removeAttributes);
                copy.AddParam(new ProvisonXmlParm() { Name = "RemoveAttributes", Value = attStr });
            
            }
            fileCopy.AddCharacteristic(copy);
            dirC.AddCharacteristic(fileCopy);
            root.AddCharacteristic(dirC);
        }


        public void AddForceDeleteOperation(string file)
        {
            var dir = Path.GetDirectoryName(file);
            var destName = Path.GetFileName(file);
            var dirC = new ProvisonXmlCharacteristic() { Type = dir, Translation = "install" };
            var fileCopy = new ProvisonXmlCharacteristic() { Type = destName };
            var copy = new ProvisonXmlCharacteristic() { Type = "Delete" };
            copy.AddParam(new ProvisonXmlParm() { Name = "ForceDelte" });
            fileCopy.AddCharacteristic(copy);
            dirC.AddCharacteristic(fileCopy);
            root.AddCharacteristic(dirC);
        }

        public void AddDeleteDirectoryOperation(string dirPath)
        {
            var dirC = new ProvisonXmlCharacteristic() { Type = dirPath, Translation = "install" };
            dirC.AddCharacteristic(new ProvisonXmlCharacteristic(){Type = "RemoveDir"});
            root.AddCharacteristic(dirC);
        }

        public void AddMoveOperation(string source, string dest, ProvisionXmlFileAttributes removeAttributes = ProvisionXmlFileAttributes.None)
        {
            var dir = Path.GetDirectoryName(dest);
            var destName = Path.GetFileName(dest);
            var dirC = new ProvisonXmlCharacteristic() { Type = dir, Translation = "install" };
            var filemove = new ProvisonXmlCharacteristic() { Type = destName };
            var move = new ProvisonXmlCharacteristic() { Type = "Move" };
            move.AddParam(new ProvisonXmlParm() { Name = "Source", Value = source, Translation = "install" });
            if (removeAttributes != ProvisionXmlFileAttributes.None)
            {
                var attStr = GetAttributeString(removeAttributes);
                move.AddParam(new ProvisonXmlParm() { Name = "RemoveAttributes", Value = attStr });
            }

            filemove.AddCharacteristic(move);
            dirC.AddCharacteristic(filemove);
            root.AddCharacteristic(dirC);
        }

        private string GetAttributeString(ProvisionXmlFileAttributes removeAttributes)
        {
            var a = (removeAttributes & ProvisionXmlFileAttributes.Archiv) == ProvisionXmlFileAttributes.Archiv ? "A" : "";
            var h = (removeAttributes & ProvisionXmlFileAttributes.Hidden) == ProvisionXmlFileAttributes.Hidden ? "H" : "";
            var r = (removeAttributes & ProvisionXmlFileAttributes.ReadOnly) == ProvisionXmlFileAttributes.ReadOnly ? "R" : "";
            var s = (removeAttributes & ProvisionXmlFileAttributes.System) == ProvisionXmlFileAttributes.System ? "S" : "";
            return a + h + r + s;
        }

        public override string ToString()
        {
            return provison.ToString();
        }
    }

    [Flags]
    public enum ProvisionXmlFileAttributes
    {
        None = 0,
        Archiv, Hidden, ReadOnly ,System 
    }
}